package com.yyk.yykywd.ui.work;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.base.BaseRecyclerAdapter;
import com.yyk.yykywd.base.BaseRecyclerHolder;
import com.yyk.yykywd.config.Constant;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.bean.WorkBusinesslistInfo;
import com.yyk.yykywd.listenter.ToolbarListenter;
import com.yyk.yykywd.ui.MainActivity;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.widght.diver.CustomDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 待签合同
 */
public class SignedContractActivity extends BaseActivity {
    public static SignedContractActivity intance;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.ll_emptyview)
    LinearLayout llEmptyview;
    private BaseRecyclerAdapter<WorkBusinesslistInfo.DataBean> adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_signed_contract;
    }

    @Override
    public void initView() {
        initToolbar();

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        startAct(MainActivity.class, true);
    }

    private void initToolbar() {
        setBackArrow(new ToolbarListenter() {
            @Override
            public void onClickListenter() {
                startAct(MainActivity.class, true);

            }
        });
        setTitle("待签合同");
    }

    @Override
    protected void onResume() {
        super.onResume();
        postWorkBusinesslist(true);
    }

    @Override
    public void initData() {
        intance = this;
        initGetIntent();
        initAdapter();

    }

    private void initGetIntent() {

    }

    private List<WorkBusinesslistInfo.DataBean> list = new ArrayList<>();

    private void initAdapter() {
        adapter = new BaseRecyclerAdapter<WorkBusinesslistInfo.DataBean>(mActivity, list, R.layout.adapter_item_signedcontract) {
            @Override
            public void convert(BaseRecyclerHolder holder, WorkBusinesslistInfo.DataBean item, int position, boolean isScrolling) {
                holder.setImageByUrl(R.id.iv_img, item.getImg());
                holder.setText(R.id.tv_uname, item.getUname());
                holder.setText(R.id.tv_topname, item.getTopname());
                holder.setText(R.id.tv_createtime, item.getCreatetime());
                holder.setText(R.id.tv_hname, "目标机构：" + item.getHname());
                holder.setText(R.id.tv_ftime, "预计拜访时间：" + item.getFtime());
                holder.setText(R.id.tv_fname, "拜访人员：" + item.getFname());
                holder.setText(R.id.tv_num, "跟进次数：" + item.getNum());
            }
        };
        rvList.addItemDecoration(new CustomDecoration(mActivity, CustomDecoration.VERTICAL_LIST, R.drawable.divider_f8f_10, 0));
        rvList.setLayoutManager(new LinearLayoutManager(mActivity));
        rvList.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", list.get(position).getId() + "");
                startAct(QuerySignedctActivity.class, bundle);
            }
        });

    }

    /**
     * 接口15：仍需跟进列表  代签合同列表  回款列表  合作暂未达成列表（whm）
     */
    private void postWorkBusinesslist(final boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("uid", Hawk.get("uid")+"");
        map.put("page", 1 + "");
        map.put("limit", 100 + "");
        map.put("type", 3 + ""); //代签合同

        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postWorkBusinesslist(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<WorkBusinesslistInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(WorkBusinesslistInfo info) {
                        Logger.e("待签合同" + info.toString());
                        if (info.getCode() == 1) {
                            llEmptyview.setVisibility(View.GONE);
                            rvList.setVisibility(View.VISIBLE);
                            if (flag) {
                                list.clear();
                            }
                            if (info.getData() != null & info.getData().size() > 0) {
                                list.addAll(info.getData());
                            }

                            adapter.notifyDataSetChanged();
                        }else if (info.getCode()==2){
                            llEmptyview.setVisibility(View.VISIBLE);
                            rvList.setVisibility(View.GONE);

                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
